File Permissions
Introduction
File permissions are expressed with a 10 character string.
To see the file permissions for files in a directory run ls -l.
The output will resemble the following:
drwxr-xr-x 2 josh josh 4096 Nov 29 15:32 Desktop
drwxr-xr-x 3 josh josh 4096 Nov 30 11:14 Documents
drwxr-xr-x 2 josh josh 4096 Nov 29 11:26 DownloadsIf we were to turn this output into a table where each column is labeled, we would see that the,first set of characters such as denotes the file permissions, second column represents the number of links to that file, the third and fouth columns denote the user and group assigned to the file, the fifth column represents the size for that file in bytes, the sixth column is the last date the file was modified, and the final column is the name of the file (directories in this case).
| Permissions | Num. Links | User Owner | Group Owner | Size (bytes) | Date Modified | File/Dir Name |
|---|---|---|---|---|---|---|
| drwxr-xr-x | 2 | josh | josh | 4096 | Nov 29 15:32 | Desktop |
| drwxr-xr-x | 3 | josh | josh | 4096 | Nov 30 11:14 | Documents |
| drwxr-xr-x | 2 | josh | josh | 4096 | Nov 29 11:26 | Downloads |
File Permission Breakdown
Let’s break down the permission string drwxr-xr-x.
First Character
The 1st character in this string is the type of file. The most common characters you will see here are - for a regular file, d for directory, and l for link. Note that a link, or symbolic link, is conceptually similar to a “shortcut” on a Windows machine.
| Symbol | Meaning |
|---|---|
- |
a regular file |
d |
a directory |
l |
a symbolic link |
In the permission string drwxr-xr-x, the first character is d, so we have a directory.
Remaining Characters
The remaining 9 characters are to denote the permissions for 3 different permission classes
The first set of 3 characters (character 2, 3, and 4) denote the permisions for the user that owns the file. In the string
drwxr-xr-x, we haverwxwhich means the owner can read, write, and execute the file.The second set of 3 characters (character 5, 6, and 7) denote the permisions for the group that owns the file. Note that each user gets a group named after themselves. Also, users can be added to other groups. You can set permissions for an entire group.
In the string
drwxr-xr-x, we haver-xas the 5th, 6th, and 7th characters. This means any user in the group that owns this file can read and execute the file.The final set of 3 characters are for all other users (those not in the group that owns the file, and not the user that owns the file)
In the string
drwxr-xr-x, we haver-xwhich means all others can read and execute the file, but cannot modify it.
Changing File Permissions
We use the command chmod to change the file permissions. You can use either of the following methods to change the permissions.
Using the characters that represent the permissions
The characters for each type of user are:
ufor ownergfor groupofor othersafor all
The characters for permissions are:
rfor readwfor writexfor execute
To add permissions, combine a usertype character with + and one or more of the permission characters.
To remove permissions, combine a usertype character with - and one or more of the permission characters.
Samples
chmod u+rwx filenameto give read write and execute permissions to the owner.chmod g-x filenameto remove execute permissions from the group.
Example
Navigate to a directory to work with. You may need to create one.
cd ~/Desktop/programs/bash-scriptsCreate a bash script we can work with.
echo "echo hello permissions" > permission-ex.shView the permissions for all the files in this directory, or just the bash file.
ls -l # all the files ls -l permission-ex.sh # just permissions for that one fileNotice it was not executable by the user.
Try executing it just to see the error.
./permission-ex.sh # attempt to execute the file, get an errorAdd execution permission and try to execute it again.
chmod u+x # allow the user to execute the file ls -l permission-ex.sh # show the permissions again ./permission-ex.sh # execute successfully
Using Numeric Values
The values for each type of permission are:
r = 4
w = 2
x = 1
You can place them adjacent to each other to get combos
rw = 4 + 2 = 6
rwx = 4 + 2 + 1 = 7
wx = 2 + 1 = 3
rx = 4 + 1 = 5
When referencing the permission string, notice that this is simply binary on a set of 3 characters. For example, with the string drwxr-xr-x we have:
d r w x r - x r - x
4 2 1 4 1 4 1
Sum: 7 5 5
Permission is 755
Sample
chmod 766 filenameto give:rwxpermissions to the ownerrwpermissions to the group
rwpermissions to the others
Example
Create a file to work with.
cd ~/Desktop/programs/bash-scripts echo "echo hello from bash" > perm-ex-2.shList the permissions.
ls -l perm-ex-2.shAllow the user to execute the file, group to read and execute, others to read and execute
chmod 755 perm-ex-2.shView the permissions and execute the file.
ls -l ./perm-ex-2.sh
Changing Ownership
The command chown will change the ownership of a file
Format:
sudo chown [new_owner] [filename]
sudo chown :[new_group] [filename]
sudo chown [new_owner] :[new_group] [filename]
Example:
Create a new user.
sudo useradd betty # create a new userSet the password for the new user. Note that you will be prompted for a password after executing the following command.
sudo passwd betty # set the password for the new userSet the user for the
perm-ex-2.shfile.sudo chown betty perm-ex-2.shSet the group for the
perm-ex-2.shfile.sudo chown :betty perm-ex-2.sh